package com.shiwaixiangcun.customer.presenter.impl;

import android.content.Context;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.GlobalConfig;
import com.shiwaixiangcun.customer.entity.ResidentBean;
import com.shiwaixiangcun.customer.presenter.IResidentCertificationPresenter;
import com.shiwaixiangcun.customer.ui.IResifdentView;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.LoginOutUtil;
import com.shiwaixiangcun.customer.utils.RefreshTokenUtil;

/**
 *
 * @author Administrator
 * @date 2017/5/25
 */

public class ResidentCertificationImpl implements IResidentCertificationPresenter {
    private static String BUG_TAG = "ResidentCertificationImpl";
    private IResifdentView iResifdentView;
    private String str_content;

    public ResidentCertificationImpl(IResifdentView iResifdentView, String strContent) {
        this.iResifdentView = iResifdentView;
        this.str_content = strContent;
    }

    @Override
    public void setBgaAdpaterAndClick(Context context) {
        sendResidentHttp(context);
    }


    //房产认证
    private void sendResidentHttp(final Context context) {

        final String refreshToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.Refresh_token, "");

        String tokenString = (String) AppSharePreferenceMgr.get(context, GlobalConfig.TOKEN, "");

        int siteId = (int) AppSharePreferenceMgr.get(context, GlobalConfig.CURRENT_SITE_ID, 20);
        OkGo.<String>get(GlobalApi.INSTANCE.getAreaTree())
                .params("access_token", tokenString)
                .params("fields", "id,name")
                .params("siteId", siteId)
                .execute(new StringCallback() {
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {

                        ResidentBean residentBean = new Gson().fromJson(response.body(), ResidentBean.class);
                        if (residentBean == null) {
                            return;
                        }
                        switch (residentBean.getResponseCode()) {
                            case 1001:
                                iResifdentView.setBgaAdpaterAndClickResult(residentBean);
                                break;
                            case 1018:

                                RefreshTokenUtil.refreshToken(context, refreshToken);
                                break;
                            case 1019:
                                LoginOutUtil.sendLoginOutUtil(context);
                                break;
                            default:
                                break;

                        }
                    }
                });

    }


}
